var serverGameController = Class.extend({
	palka1: null,
	palka2: null,
	player1: null,
	player2: null,
	timer : null,
	ball : null,
	width: 640,
	height: 400,
	startSpeed: 27,
	len1: 100,
	len2: 100,
	init : function(player1, player2){
		this.player1=player1;
		this.player2=player2;
		this.ball=new ball(this.width/2, this.height/2);
		this.palka1=new palka(10, this.height/2-this.len1/2, this.len1);
		this.palka2=new palka(this.width/2-10, this.height/2-this.len2/2, this.len2);
		this.player1.json.send({'event': 'ballUpDate', 'ball': this.ball});
		this.player2.json.send({'event': 'ballUpDate', 'ball': this.ball});
		this.player1.json.send({'event': 'palka1UpDate', 'palka1': this.palka1});
		this.player2.json.send({'event': 'palka1UpDate', 'palka1': this.palka1});
		this.player1.json.send({'event': 'palka2UpDate', 'palka2': this.palka2});
		this.player2.json.send({'event': 'palka2UpDate', 'palka2': this.palka2});
	},
	start:function(){
		if((this.ball.speedX*this.ball.speedX+this.ball.speedY*this.ball.speedY)==0){
			var rand=(new Date).getMilliseconds%2-1;
			this.ball.speedX=rand*this.startSpeed;
			this.ball.speedY=1;
		};
		this.player1.json.send({'event': 'ballUpDate', 'ball': this.ball.speedX});
		this.player2.json.send({'event': 'ballUpDate', 'ball': this.ball.speedX});
		this.player1.on('moveUp', function(){
			this.palka1.speed=1;
			this.player1.json.send({'event': 'move', 'player': true, 'up': true});
			this.player2.json.send({'event': 'move', 'player': true, 'up': true});
		});
		this.player2.on('moveUp', function(){
			this.palka2.speed=1;
			this.player1.json.send({'event': 'move', 'player': false, 'up': true});
			this.player2.json.send({'event': 'move', 'player': false, 'up': true});
		});
		this.player1.on('moveDown', function(){
			this.palka1.speed=-1;
			this.player1.json.send({'event': 'move', 'player': true, 'up': false});
			this.player2.json.send({'event': 'move', 'player': true, 'up': false});
		});
		this.player2.on('moveDown', function(){
			this.palka2.speed=-1;
			this.player1.json.send({'event': 'move', 'player': false, 'up': false});
			this.player2.json.send({'event': 'move', 'player': false, 'up': false});
		});
		this.player1.on('stopMove', function(){
			this.palka1.speed=0;
			this.player1.json.send({'event': 'palka1UpDate', 'palka1': this.palka1});
			this.player2.json.send({'event': 'palka1UpDate', 'palka1': this.palka1});
		});
		this.player2.on('stopMove', function(){
			this.palka2.speed=0;
			this.player1.json.send({'event': 'palka2UpDate', 'palka2': this.palka2});
			this.player2.json.send({'event': 'palka2UpDate', 'palka2': this.palka2});
		});
		this.timer=setInterval(this.onTimer.inScope(this),40);
	},
	onTimer : function(){
		this.player1.json.send({'event': 'palka2UpDate', 'palka2': this.palka2})
		this.palka1.upDate();
		this.palka2.upDate();
		var x=this.ball.x;
		var y=this.ball.y;
		this.ball.x+=this.ball.speedX;
		this.ball.y+=this.ball.speedY;
		var point=null;
		if(this.ball.y<0){
			this.ball.speedY=-this.ball.speedY;
			this.ball.y=-this.ball.y;
		}
		if(this.ball.y>this.height){
			this.ball.speedY=-this.ball.speedY;
			this.ball.y=2*this.height-this.ball.y;
		}
		if(x>this.ball.x){
			point=segmentsIntersection(x,y,this.ball.x,this.ball.y+this.ball.r/3,
				this.palka1.x+this.ball.r,this.palka1.y,this.palka1.x+
				this.ball.r,this.palka1.y+this.palka1.len+this.ball.r/3);
		
			if(point){
				this.ball.speedX=-this.ball.speedX;
				this.ball.x=2*(this.palka1.x+this.ball.r)-this.ball.x;
				var k=point[1]-this.palka1.y;
				var deg=(2*k-this.palka1.len)/this.palka1.len;
				this.player1.json.send({'event': 'ballUpDate', 'ball': this.ball});
				this.player2.json.send({'event': 'ballUpDate', 'ball': this.ball});
			}
		}
		if(x<this.ball.x){
			point=segmentsIntersection(x,y,this.ball.x,this.ball.y+this.ball.r/3,
				this.palka2.x-this.ball.r,this.palka2.y,this.palka2.x-
				this.ball.r,this.palka2.y+this.palka1.len+this.ball.r/3);
		
			if(point){
				this.ball.speedX=-this.ball.speedX;
				this.ball.x=2*(this.palka2.x-this.ball.r)-this.ball.x;
				var k=point[1]-this.palka2.y;
				var deg=(2*k-this.palka2.len)/this.palka2.len;
				this.player1.json.send({'event': 'ballUpDate', 'ball': this.ball});
				this.player2.json.send({'event': 'ballUpDate', 'ball': this.ball});
			}
		}
		if(this.ball.x<0){
			this.ball.speedX=-this.ball.speedX;
			this.ball.x=-this.ball.x;
			this.player1.json.send({'event': 'scoreUpDate', 'player': true});
			this.player2.json.send({'event': 'scoreUpDate', 'player': true});
		}
		if(this.ball.x>this.width){
			this.ball.speedX=-this.ball.speedX;
			this.ball.x=this.width*2-this.ball.x;
			this.player1.json.send({'event': 'scoreUpDate', 'player': false});
			this.player2.json.send({'event': 'scoreUpDate', 'player': false});
		}
	}
});